<?php

/**
 * Description of BaseController
 *
 * @author useradmin
 *
 * @category action
 * @package action
 * @subpackage common
 *
 */

class BaseController
{

    protected static $tpl = null;
    protected $member_info;
    protected $unionLoginInfo;

    protected $username_id;//登录用户ID
    protected $directoryId;//目录ID
    protected $user;//登录用户信息
    protected $left_menu;//目录
    public function __construct($themes = '')
    {
       !$themes && $themes = APP_NAME;

        if ($this->tpl === NULL)
            $this->tpl = new SmartyCap(ROOT_PATH, $themes);
        $this->tpl->assign("http_url", HTTPFILEURL); //
        $this->competence();
    }

    private function getip(){
        if($_SERVER['REMOTE_ADDR']) {
                $cip= $_SERVER['REMOTE_ADDR'];
            }elseif(getenv("REMOTE_ADDR")) {
                $cip= getenv("REMOTE_ADDR");
            }elseif(getenv("HTTP_CLIENT_IP")) {
                $cip= getenv("HTTP_CLIENT_IP");
            }else{
                $cip= "unknown";
            }
            return $cip;
    }
    private function competence(){
        $cookie_time=Handler_http::getCookie('login_out');
        if(!empty($cookie_time)){
            $lifeTime = SITE_TIME;
            setcookie('login_out', '1', time() + $lifeTime,'/');
            session_start();
            $this->username_id = Handler_http::getSession('PHPSESSID');
            $con=AdminLoginModule::getAdminDirectory($this->username_id);

            $this->directoryId=$con[0]['directory_id'];
            $this->createTime= $con[0]['joined_time'];

            $time=AdminLoginModule::sqlPwdTime($this->username_id);
            //从数据库中取出时间，计算指定的日期是否在当前时间内
            $SQLTIME=$time[0]['pwd_time']+EXPIRED_TIME*24*3600;
            if($SQLTIME < time()){
                Handler_http::setSession('PHPTYPEID', true);
                Handler_tool::alertAndRedirectTop('您的密码已过期,请修改密码后再登陆!!', HTTPFILEURL.'/admin_index/');
            }
            $users = Handler_http::getSession('txtUsername');//登录用户名
            $this->user = AdminLoginModule::select_cookie($this->username_id);
                if($this->user['is_oper'] != "1" || (!empty($this->user['ip_adress']) && $this->user['ip_adress'] != AdminLoginModule::getUserIp())){
                     Handler_http::delSession('txtUsername');
                    Handler_http::delSession('txtPassword');
                    Handler_http::delSession('PHPSESSID');
                    exit(Handler_tool::alertAndRedirectTop('您的帐号不可进入本系统，请联系管理员！', HTTPFILEURL.'/login/'));
                }
            //用户权限验证[S]
            $url_this = substr(dirname($_SERVER['REQUEST_URI']), 1);
            if(strrpos($url_this, "/") !== false ){
                 $exp = explode("/", $url_this);
                 $url_this=$exp[0];
            }
            $row_type = AdminLoginModule::select_grouproot($this->username_id);
            if(!$row_type){
                Handler_http::delSession('txtUsername');
                Handler_http::delSession('txtPassword');
                Handler_http::delSession('PHPSESSID');
                Handler_tool::alertAndRedirectTop('该用户所属用户组不可用，请联系管理员重新分配！', HTTPFILEURL.'/login/');
            }
            $array_url = array();
            $user_menu = NavMenuModule::get_user_menu($row_type);
            if(!empty($user_menu)){
                foreach($user_menu as $row){
                    foreach ($row['sub_title'] as $sub) {
                        $array_url[] = dirname($sub['url']);
                    }
                }
            }
            if (!in_array($url_this, $array_url)) {
                Handler_tool::alertAndRedirectTop('您没有访问该页面的权限!!', HTTPFILEURL.'/login/');
            }
            //用户权限验证[END]
            // 登陆校验
            // 如果未登陆,调转  到登陆页面。跳转url支持回调地址
            // 如果已登陆继续执行程序
            if (is_array($this->user)) {
                if (!AdminLoginModule::checkdMd5($users, $this->user['username'])) {
                    Handler_tool::alertAndRedirectTop('您没有访问该页面的权限!!', HTTPFILEURL.'/login/');
                }
            } else {
                Handler_tool::alertAndRedirectTop('您没有访问该页面的权限!!', HTTPFILEURL.'/login/');
            }
        }else{
            session_start();
            session_destroy();
            Handler_tool::alertAndRedirectTop('超时访问!!', HTTPFILEURL.'/login/');
        }
    }

}

?>
